package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.HotChannel;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/6/28.
 */
public class HotBottonAdapter extends RecyclerView.Adapter<HotBottonAdapter.HotBottonViewHolder>{

    private Context context;
    private LayoutInflater inflater;
    private List<HotChannel> hotChannelList;

    private mItemClickListener itemClickListener;

    public HotBottonAdapter(Context context, List<HotChannel> hotChannelList) {
        this.context = context;
        this.hotChannelList = hotChannelList;

        inflater  = LayoutInflater.from(context);
    }

    /**
     * 设置监听事件
     * @param listener
     */
    public void setOnItemClickListener(mItemClickListener listener)
    {
        this.itemClickListener = listener;
    }

    @Override
    public HotBottonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View itemView = inflater.inflate(R.layout.adapter_hot_botton,null);
        return new HotBottonViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(HotBottonViewHolder holder, final int position) {

        HotChannel hotChannel = hotChannelList.get(position);
        ImageLoader.getInstance().displayImage(hotChannel.getImage_url(),holder.imageView, ImageUtil.getDefaultOptions());
        holder.tvtitle.setText(hotChannel.getTitle());
        holder.tvpalynum.setText(hotChannel.getPlay_num()+"");

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return hotChannelList == null ? 0 : hotChannelList.size();
    }

    public class HotBottonViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView tvtitle,tvpalynum;
        public HotBottonViewHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.adapter_hot_botton_iv);
            tvtitle = (TextView) itemView.findViewById(R.id.adapter_hot_botton_title_tv);
            tvpalynum = (TextView) itemView.findViewById(R.id.adapter_hot_botton_playnum_tv);
        }
    }

    public interface mItemClickListener
    {
        void onItemClick(int position);
    }
}
